package com.ruoyi.web.controller.teacher;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.domain.*;
import io.swagger.models.auth.In;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.service.IBasisProcessDataService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 过程数据管理Controller
 * 
 * @author wangzebing
 * @date 2025-03-28
 */
@RestController
@RequestMapping("/data")
public class BasisProcessDataController extends BaseController
{
    @Autowired
    private IBasisProcessDataService basisProcessDataService;

    /**
     * 查询过程数据管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BasisProcessData basisProcessData)
    {
        startPage();
        List<BasisProcessData> list = basisProcessDataService.selectBasisProcessDataList(basisProcessData);
        return getDataTable(list);
    }

    /**
     * 导出过程数据管理列表
     */
    @Log(title = "过程数据管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BasisProcessData basisProcessData)
    {
        List<BasisProcessData> list = basisProcessDataService.selectBasisProcessDataList(basisProcessData);
        ExcelUtil<BasisProcessData> util = new ExcelUtil<BasisProcessData>(BasisProcessData.class);
        util.exportExcel(response, list, "过程数据管理数据");
    }

    /**
     * 获取过程数据管理详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(basisProcessDataService.selectBasisProcessDataById(id));
    }

    /**
     * 新增过程数据管理
     */
    @Log(title = "过程数据管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BasisProcessData basisProcessData)
    {
        return toAjax(basisProcessDataService.insertBasisProcessData(basisProcessData));
    }

    /**
     * 修改过程数据管理
     */
    @Log(title = "过程数据管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BasisProcessData basisProcessData)
    {
        return toAjax(basisProcessDataService.updateBasisProcessData(basisProcessData));
    }

    /**
     * 删除过程数据管理
     */
    @Log(title = "过程数据管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(basisProcessDataService.deleteBasisProcessDataByIds(ids));
    }

    /**
     * 查询控制命令参数管理列表
     */
    @GetMapping("/getCommandList")
    public TableDataInfo getCommandList(BasisProcessDataBo basisProcessData)
    {
        startPage();
        List<BaseBehaviormanagementLog> list = basisProcessDataService.getCommandList(basisProcessData);
        return getDataTable(list);
    }

    /**
     * 查询接收效果数据管理列表
     */
    @GetMapping("/getAcceptEffectList")
    public TableDataInfo getAcceptEffectList(BasisProcessDataBo basisProcessData)
    {
        List<Integer> userIdList = basisProcessDataService.getUserList();
        startPage();
        List<FileEntityPo> list = basisProcessDataService.getAcceptEffectList(basisProcessData, userIdList);
        return getDataTable(list);
    }

    /**
     * 查询仿真事件数据管理列表
     */
    @GetMapping("/getSimulationEventList")
    public TableDataInfo getSimulationEventList(BasisProcessDataBo basisProcessData)
    {
        List<Integer> userIdList = basisProcessDataService.getUserList();
        startPage();
        List<BaseSignalanalysisInfo> list = basisProcessDataService.getSimulationEventList(basisProcessData, userIdList);
        return getDataTable(list);
    }

    /**
     * 文件下载
     */
    @GetMapping("/dataFileDownload")
    public ResponseEntity<InputStreamResource> dataFileDownload(@RequestParam String filePath) throws IOException {
        File file = new File(filePath);
        if (!file.exists() || !file.isFile()) {
            return ResponseEntity.notFound().build();
        }

        String encodedFileName = URLEncoder.encode(file.getName(), "UTF-8")
                .replace("+", "%20");

        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Disposition",
                "attachment; filename=\"" + file.getName() + "\"; " +
                        "filename*=UTF-8''" + encodedFileName);
        headers.add("Content-Type", MediaType.APPLICATION_OCTET_STREAM_VALUE);
        headers.add("Access-Control-Expose-Headers", "Content-Disposition");

        return ResponseEntity.ok()
                .headers(headers)
                .body(new InputStreamResource(new FileInputStream(file)));
    }
}
